public class Num8 {
    public static int listLen(ListNode head) {
        int ret = 0;
        ListNode cur = head;
        while(cur != null) {
            cur = cur.next;
            ret++;
        }
        return ret;
    }
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        int lenA = listLen(headA);
        int lenB = listLen(headB);
        int differ = Math.abs(lenA - lenB);
        ListNode curA = headA;
        ListNode curB = headB;
        if(lenB > lenA) {
            for (int i = 0; i < differ; i++) {
                curB = curB.next;
            }
        } else {
            for (int i = 0; i < differ; i++) {
                curA = curA.next;
            }
        }
        while(curA != null && curB != null) {
            if(curA == curB) {
                return curA;
            }
            curB = curB.next;
            curA = curA.next;
        }
        return null;
    }
    public ListNode getIntersectionNode2(ListNode headA, ListNode headB) {
        ListNode curA = headA;
        while(curA != null) {
            if(checkList(curA, headB)) {
                return curA;
            }
            curA = curA.next;
        }
        return null;
    }
    public static boolean checkList(ListNode l1, ListNode l2) {
        //l2 遍历
        while(l2 != null) {
            if(l2 == l1) {
                return true;
            }
            l2 = l2.next;
        }
        return false;
    }
}
